package com.sec.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sec.mapper.UserMapper;
import com.sec.pojo.User;

@Service(value="userService")
public class UserService {
	
	@Autowired
	private UserMapper userMapper;
	
	//让Spring帮助进行事务回滚
	@Transactional
	public User register(User user){
		//此处的length表示的是更改生效的行数，大于0即表示新增成功，然后user对象的id值被自动赋予新值
		int length = userMapper.insertUser(user);
		if (length>0) {
			return user;
		}else {
			//新增失败，抛出异常，让spring进行事物回滚
			throw new RuntimeException();
		}
	}
}
